/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal;

import org.eclipse.jface.window.Window;
import org.eclipse.rap.rwt.SingletonUtil;

/**
 * This handler will pass along to the workbench advisor exceptions
 * and errors thrown while running the event loop. However, the
 * <code>ThreadDeath</code> error is simply thrown again, and is not 
 * passed along.
 */
public final class ExceptionHandler implements Window.IExceptionHandler {

//RAP [if] Session scoped ExceptionHandler
//    private static final ExceptionHandler instance = new ExceptionHandler();

    /**
     * Returns the singleton exception handler.
     * 
     * @return the singleton exception handler
     */
    public static ExceptionHandler getInstance() {
// RAP [if] Session scoped ExceptionHandler
//        return instance;
        return SingletonUtil.getSessionInstance( ExceptionHandler.class );
    }

    private int exceptionCount = 0; // To avoid recursive errors

    private ExceptionHandler() {
        // prevents instantiation
    }

    /* (non-javadoc)
     * @see org.eclipse.jface.window.Window.IExceptionHandler#handleException
     */
    public void handleException(Throwable t) {
        try {
            // Ignore ThreadDeath error as its normal to get this when thread dies
            if (t instanceof ThreadDeath) {
                throw (ThreadDeath) t;
            }

            // Check to avoid recursive errors
            exceptionCount++;
            if (exceptionCount > 2) {
                if (t instanceof RuntimeException) {
					throw (RuntimeException) t;
				}
                throw (Error) t;
            }

            // Let the advisor handle this now
            Workbench wb = Workbench.getInstance();
            if (wb != null) {
                wb.getAdvisor().eventLoopException(t);
            }
        } finally {
            exceptionCount--;
        }
    }
}
